﻿using Common.Core.Exceptions;
using Microsoft.AspNetCore.JsonPatch;
using NetByDocker.Domain.Model.UserManage;
using NetByDocker.Model.UserManage;
using NetByDocker.Service.UserManage;

namespace NetByDocker.Controllers;

/// <summary>
/// 用户控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;
    private readonly IMapper _mapper;

    public UserController(IUserService userService,
                          IMapper mapper)
    {
        _userService = userService;
        _mapper = mapper;
    }

    /// <summary>
    /// 查询用户分页列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public Task<GetQueryPageResult<GetUserPageInfoResponse>> GetPageListAsync(
        [FromQuery] GetUserPageRequest request, CancellationToken cancellationToken)
    {
        return _userService.GetPageListAsync(request, cancellationToken);
    }

    /// <summary>
    /// 导出分页数据
    /// </summary>
    /// <param name="request"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<ActionResult> ExportPageListAsync(
        [FromQuery] GetUserPageRequest request, CancellationToken cancellationToken)
    {
        var info = await _userService.ExportPageListAsync(request, cancellationToken);
        return File(info.Content, info.ContentType, info.Name);
    }

    /// <summary>
    /// 查询详情
    /// </summary>
    /// <param name="id"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpGet("{id:long}")]
    public Task<GetUserInfoResponse> GetDetailsAsync([FromRoute] long id,
                                                     CancellationToken cancellationToken)
    {
        return _userService.GetDetailsAsync(id, cancellationToken);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="dto"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpPost]
    public Task<long> AddAsync([FromBody] AddUserVm dto, CancellationToken cancellationToken)
    {
        return _userService.AddAsync(dto, cancellationToken);
    }

    /// <summary>
    /// 修改账户密码
    /// </summary>
    /// <param name="id"></param>
    /// <param name="dto"></param>
    /// <param name="cancellationToken"></param>
    [HttpPut("{id:long}")]
    public Task<int> PutAsync(long id, [FromBody] UpdateUserVm dto, CancellationToken cancellationToken)
    {
        return _userService.UpdateAsync(id, dto, cancellationToken);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <param name="cancellationToken"></param>
    [HttpDelete("{id:long}")]
    public Task<int> DeleteAsync(long id, CancellationToken cancellationToken)
    {
        return _userService.DeleteAsync(id, cancellationToken);
    }

    /// <summary>
    /// 修改  PATCH仅指定更改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="jsonPatch"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpPatch("{id:long}")]
    public async Task<int> PatchAsync([FromRoute] long id,
                                      [FromBody] JsonPatchDocument<UpdateUserVm> jsonPatch, CancellationToken cancellationToken)
    {
        var entity = await _userService.GetDetailsAsync(id, cancellationToken).ConfigureAwait(false);
        if (entity is null)
            throw new NotFoundException("用户不存在");

        var dto = _mapper.Map<UpdateUserVm>(entity);
        jsonPatch.ApplyTo(dto, ModelState);

        return await _userService.PatchUpdateAsync(id, dto, cancellationToken).ConfigureAwait(false);
    }
}